package com.example.spider.config.support.method;

import jakarta.annotation.Nullable;
import org.springframework.core.MethodParameter;
import org.springframework.web.method.support.HandlerMethodArgumentResolverComposite;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @see HandlerMethodArgumentResolverComposite
 */
public class MethodArgumentResolverComposite implements MethodArgumentResolver {

	private final List<MethodArgumentResolver> argumentResolvers = new ArrayList<>();

	private final Map<MethodParameter, MethodArgumentResolver> argumentResolverCache =
			new ConcurrentHashMap<>(256);


	/**
	 * Add the given {@link MethodArgumentResolver}.
	 */
	public MethodArgumentResolverComposite addResolver(MethodArgumentResolver resolver) {
		this.argumentResolvers.add(resolver);
		return this;
	}

	/**
	 * Add the given {@link MethodArgumentResolver MethodArgumentResolvers}.
	 * @since 4.3
	 */
	public MethodArgumentResolverComposite addResolvers(
			@Nullable MethodArgumentResolver... resolvers) {

		if (resolvers != null) {
			Collections.addAll(this.argumentResolvers, resolvers);
		}
		return this;
	}

	/**
	 * Add the given {@link MethodArgumentResolver MethodArgumentResolvers}.
	 */
	public MethodArgumentResolverComposite addResolvers(
			@Nullable List<? extends MethodArgumentResolver> resolvers) {

		if (resolvers != null) {
			this.argumentResolvers.addAll(resolvers);
		}
		return this;
	}

	/**
	 * Return a read-only list with the contained resolvers, or an empty list.
	 */
	public List<MethodArgumentResolver> getResolvers() {
		return Collections.unmodifiableList(this.argumentResolvers);
	}

	/**
	 * Clear the list of configured resolvers and the resolver cache.
	 * @since 4.3
	 */
	public void clear() {
		this.argumentResolvers.clear();
		this.argumentResolverCache.clear();
	}


	/**
	 * Whether the given {@linkplain MethodParameter method parameter} is
	 * supported by any registered {@link MethodArgumentResolver}.
	 */
	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return getArgumentResolver(parameter) != null;
	}

	/**
	 * Iterate over registered
	 * {@link MethodArgumentResolver MethodArgumentResolvers}
	 * and invoke the one that supports it.
	 * @throws IllegalArgumentException if no suitable argument resolver is found
	 */
	@Override
	@Nullable
	public Object resolveArgument(MethodParameter parameter, Object... params) throws Exception {
		MethodArgumentResolver resolver = getArgumentResolver(parameter);
		if (resolver == null) {
			throw new IllegalArgumentException("Unsupported parameter type [" +
					parameter.getParameterType().getName() + "]. supportsParameter should be called first.");
		}
		return resolver.resolveArgument(parameter, params);
	}

	/**
	 * Find a registered {@link MethodArgumentResolver} that supports
	 * the given method parameter.
	 */
	@Nullable
	private MethodArgumentResolver getArgumentResolver(MethodParameter parameter) {
		MethodArgumentResolver result = this.argumentResolverCache.get(parameter);
		if (result == null) {
			for (MethodArgumentResolver resolver : this.argumentResolvers) {
				if (resolver.supportsParameter(parameter)) {
					result = resolver;
					this.argumentResolverCache.put(parameter, result);
					break;
				}
			}
		}
		return result;
	}

}
